package com.evamay.mytinkerdemo.application;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.widget.Toast;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.LogAdapter;
import com.orhanobut.logger.Logger;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.interfaces.BetaPatchListener;
import com.tencent.tinker.entry.DefaultApplicationLike;

import java.util.Locale;


public class SampleApplicationLike extends DefaultApplicationLike {


    public SampleApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag,
                applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }


    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        MultiDex.install(base);
        Beta.installTinker(this);
        Toast.makeText(getApplication(), "加载完成", Toast.LENGTH_SHORT).show();
    }


    public final void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
        getApplication().registerActivityLifecycleCallbacks(callback);
    }

    public void onCreate() {
        super.onCreate();
        Logger.addLogAdapter((LogAdapter) (new AndroidLogAdapter()));
        Beta.enableHotfix = true;
        Beta.canAutoDownloadPatch = true;
        Beta.canAutoPatch = true;
        Beta.canNotifyUserRestart = true;
        Beta.autoCheckUpgrade=true;
        Beta.betaPatchListener = new BetaPatchListener() {
            @Override
            public void onPatchReceived(String patchFile) {
                Toast.makeText(getApplication(), "补丁下载地址" + patchFile, Toast.LENGTH_SHORT).show();
                Logger.e("补丁下载地址：" + patchFile);
            }

            @Override
            public void onDownloadReceived(long savedLength, long totalLength) {
                String progress =String.format(Locale.getDefault(), "%s %d%%",
                        Beta.strNotificationDownloading,
                        (int) (totalLength == 0 ? 0 : savedLength * 100 / totalLength));

                Toast.makeText(getApplication(), progress, Toast.LENGTH_SHORT).show();
                Logger.e("下载进度： " + progress);
            }

            @Override
            public void onDownloadSuccess(String msg) {
                Toast.makeText(getApplication(), "补丁下载成功", Toast.LENGTH_SHORT).show();
                Logger.e("补丁下载成功：" + msg);
            }

            @Override
            public void onDownloadFailure(String msg) {
                Toast.makeText(getApplication(), "补丁下载失败", Toast.LENGTH_SHORT).show();
                Logger.e("补丁下载失败：" + msg);

            }

            @Override
            public void onApplySuccess(String msg) {
                Toast.makeText(getApplication(), "补丁应用成功", Toast.LENGTH_SHORT).show();
                Logger.e("补丁应用成功"+msg);
            }

            @Override
            public void onApplyFailure(String msg) {
                Toast.makeText(getApplication(), "补丁应用失败", Toast.LENGTH_SHORT).show();
                Logger.e("补丁应用失败：" + msg);
            }

            @Override
            public void onPatchRollback() {
                Logger.e("补丁回滚：" );
            }
        };

        Bugly.setIsDevelopmentDevice(getApplication(), true);
        Bugly.init(getApplication(), "0e7da12912", true);
    }

    public void onTerminate() {
        super.onTerminate();
        Beta.unInit();
    }
}